package czk.concrent.threadpool.hungry.practice;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 由于线程的缺少所以会一直处于阻塞状态
 * @Author:ChenZhangKun
 * @Date: 2020/8/24 23:35
 */
public class ThreadHungryPractice {
    // 定义菜单
    private static List<String> order = Arrays.asList("香菇鸡", "清蒸鲈鱼", "啤酒鸭", "红烧肉");
    public static void main(String[] args) {
      // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(2);
         executorService.submit(() -> {
            System.out.println("处理点菜");
            // 做菜
            Future<String> submit = executorService.submit(() -> {
                String food = getFood();
                return food;
            });
             try {
                 String s = submit.get();
                 System.out.println(s+"出锅了");
             } catch (InterruptedException e) {
                 e.printStackTrace();
             } catch (ExecutionException e) {
                 e.printStackTrace();
             }
         });
        executorService.submit(() -> {
            System.out.println("处理点菜");
            // 做菜
            Future<String> submit = executorService.submit(() -> {
                String food = getFood();
                return food;
            });
            try {
                String s = submit.get();
                System.out.println(s+"出锅了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 获取菜
     * @return
     */
    public static String getFood(){
        return order.get(new Random().nextInt(4));
    }
}
